<?php
class ControllerCamdoBiennhan extends Controller
{
	private $error = array();
   	function __construct() 
	{
	 	$this->load->model("camdo/biennhan");
		$this->load->model("ben/thuchi");
		$this->data['data_luutru'] = $this->document->getLuuTrus();
		
   	}
	
	public function index()
	{
		$this->getList();
	}
	
	public function insert()
	{
    	$this->getForm();
	}
	
	public function update()
	{
		$this->data['haspass'] = false;
		$this->data['readonly'] = 'readonly="readonly"';
		$this->data['class'] = 'readonly';	
		$this->getForm();
  	}
	
	
	
	public function delete() 
	{
		$listbiennhanid=$this->request->post['delete'];
		
		if(count($listbiennhanid))
		{
			foreach($listbiennhanid as $biennhanid)
			{
				$this->model_camdo_biennhan->delete($biennhanid);	
			}
			$this->data['output'] = "true";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	public function getList() 
	{
		$this->data['insert'] = $this->url->http('camdo/biennhan/insert');
		$this->data['delete'] = $this->url->http('camdo/biennhan/delete');		
		
		$this->data['datas'] = array();
		$where = "";
		$data = $this->request->get;
		foreach($data as $key =>$val)
		{
			$data[$key] = urldecode($val);	
		}
		$_GET = $data;
		if(trim($data['sobiennhan']))
		{
			$where .= " AND sobiennhan like '".$data['sobiennhan']."'";
		}
		
		if(trim($data['tungay']))
		{
			$where .= " AND ngaylap >= '".$this->date->formatViewDate($data['tungay'])."'";
		}
		
		if(trim($data['denngay']))
		{
			$where .= " AND ngaylap <= '".$this->date->formatViewDate($data['denngay'])."'";
		}
		
		if(trim($data['tenkhachhang']))
		{
			$where .= " AND tenkhachhang like '%".$data['tenkhachhang']."%'";
		}
		
		if(trim($data['sotientu']))
		{
			$where .= " AND tongtien >= '".$this->string->toNumber($data['sotientu'])."'";
		}
		
		if(trim($data['sotienden']))
		{
			$where .= " AND tongtien <= '".$this->string->toNumber($data['sotienden'])."'";
		}
		
		
		if(trim($data['luutru']))
		{
			$where .= " AND luutru like '".$data['luutru']."'";
		}
		
		$where .= " Order by ngaylap DESC";
		$rows = $this->model_camdo_biennhan->getList($where);
		if(trim($data['tinhtrang']))
		{
			//$where .= " AND tinhtrang like '".$data['tinhtrang']."'";
			$data_biennhan = array();
			foreach($rows as $item)
			{
				if($this->document->getTinhTrangCamDo($item) == $data['tinhtrang'])
				{
					$data_biennhan[] = $item;	
				}
			}
			$rows = $data_biennhan;
		}
		
		//Page
		$page = $this->request->get['page'];		
		$x=$page;
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		//for($i=0;$i < count($rows[$i]);$i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['link_edit'] = $this->url->http('camdo/biennhan/update&biennhanid='.$this->data['datas'][$i]['biennhanid']);
			$this->data['datas'][$i]['text_edit'] = "Sửa";
			
			
			
		}
		
		$this->id='content';
		$this->template="camdo/biennhan_list.tpl";
		$this->layout=$this->user->getLayout();
		
		$this->render();
	}
	
	
	private function getForm()
	{		
		
		if ((isset($this->request->get['biennhanid'])) ) 
		{
      		$this->data['item'] = $this->model_camdo_biennhan->getItem($this->request->get['biennhanid']);
			$where = " AND biennhanid = '".$this->request->get['biennhanid']."'";
			//$this->data['data_chitiet'] = $this->model_camdo_biennhan->saveBienNhanLichSu($where);
			
    	}
		else
		{
			$this->data['item']['ngaycam'] = $this->date->getToday();
			$this->data['item']['ngaydenlai'] = $this->date->addday($this->data['item']['ngaycam'],30);
			$this->data['item']['ngayquahantoida'] = 15;
		}
		
		$this->id='content';
		$this->template='camdo/biennhan_form.tpl';
		$this->layout=$this->user->getLayout();
		$this->render();
	}
	
	public function view()
	{
		$biennhanid = $this->request->get['biennhanid'];
		$this->data['item'] = $this->model_camdo_biennhan->getItem($biennhanid);
		$where = " AND biennhanid = '".$this->request->get['biennhanid']."'";
		$this->data['data_chitiet'] = $this->model_camdo_biennhan->getBienNhanLichSuList($where);
		$this->data['today'] = $this->date->getTodayNoTime();
		$time = $this->date->_compareDate($this->data['today'],$this->data['item']['ngaycam']);
		
		$this->data['songay'] = $this->date->intToDay($time);
		$this->data['songaytinhlai'] = $this->getSoNgayTinhLai($this->data['item'],$this->data['songay']);
		
		$this->id='content';
		$this->template='camdo/biennhan_view.tpl';
		if($_GET['dialog']=='print')
		{
			$this->template='camdo/biennhan_print.tpl';
			$this->layout='layout/print';
		}
		$this->render();
	}
	
	public function tinhlai()
	{
		$biennhanid = $this->request->get['biennhanid'];
		$ngaydonglai = $this->date->formatViewDate($this->request->get['ngaydonglai']);
		
		$biennhan = $this->model_camdo_biennhan->getItem($biennhanid);
		if($biennhan['ngaydonglai'] == '0000-00-00')
			$biennhan['ngaydonglai'] = $biennhan['ngaycam'];
		$time = $this->date->_compareDate($ngaydonglai,$biennhan['ngaydonglai']);
		
		$songay = $this->date->intToDay($time);
		$songaytinhlai = $this->getSoNgayTinhLai($biennhan,$songay);
		$lai = $biennhan['giatricam']*$biennhan['laisuat']/100/30*$songaytinhlai;
		
		$data = array(
						'songay' => $songay,
						'songaytinhlai' => $songaytinhlai,
						'lai' => $lai,
						'vonlai' => $biennhan['giatricam'] + $lai
						);
		
		
		$this->data['output'] = json_encode($data);
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	function giamlai()
	{
		$biennhanid = $this->request->get['biennhanid'];
		$giamlai = ($this->request->get['giamlai']=="")?2:1;
		$this->model_camdo_biennhan->updateCol($biennhanid,'giamlai',$giamlai);
		$this->data['output'] = "true";
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	function getSoNgayTinhLai($biennhan,$songay)
	{
		if($biennhan['giamlai']==1)
		{
			return $songay;
		}
		else
		{
			$arr_khungthoigian = split(",", $this->document->setup['khungthoigian']);
			foreach($arr_khungthoigian as $val)
			{
				if($val>=$songay)
					return $val;	
			}
		}
		return $songay;
	}
	
	public function updateTinhTrang()
	{
		$data = $this->request->post;
		$this->model_camdo_biennhan->updateCol($data['biennhanid'],'tinhtrang',$data['tinhtrang']);
		$this->data['output'] = "true";
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	public function thanhtoan()
	{
		$data = $this->request->post;
		
		if($this->validateFormThanhToan($data))
		{
						
			$biennhan = $this->model_camdo_biennhan->getItem($data['biennhanid']);
			
			//Xuat phieu thu tien tam ung
			$phieuthu['prefix'] = "PT";
			$phieuthu['loaithuchi'] = "thu";
			$phieuthu['taikhoanthuchi'] = "thutienbienlai";
			$phieuthu['chungtulienquan'] = $biennhan['sobiennhan'];
			$phieuthu['makhachhang'] = $biennhan['khachhangid'];
			$phieuthu['tenkhachhang'] = $biennhan['tenkhachhang'];
			$phieuthu['diachi'] = $biennhan['diachi'];
			$phieuthu['email'] = $biennhan['sobiennhan'];
			$phieuthu['dienthoai'] = $biennhan['sodienthoai'];
			$phieuthu['email'] = $biennhan['email'];
			$phieuthu['sotien'] = $this->string->toNumber($data['thanhtoan']);
			$phieuthu['donvi'] = 'VND';
			$phieuthu['quidoi'] = $this->document->toVND($this->string->toNumber($phieuthu['sotien']),$phieuthu['donvi']);
			$phieuthu['lydo'] = "Thu tiền biên nhận ".$biennhan['sobiennhan'];
			$phieuthu['nguongoc'] = $biennhan['biennhanid'];
			$phieuthu['maphieu'] = $this->model_ben_thuchi->insert($phieuthu);	
				
			
			$this->data['output'] = "true";
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."\n";
			}
		}
		
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateFormThanhToan($data)
	{
		$biennhan = $this->model_camdo_biennhan->getItem($data['biennhanid']);
		if (trim($data['thanhtoan']) == 0) 
		{
      		$this->error['thanhtoan'] = "Bạn chưa nhập số tiền thanh toán";
    	}
		
		
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	
	public function save()
	{
		$data = $this->request->post;
		
		if($this->validateForm($data))
		{
			$data['giamlai'] = ($data['giamlai']=="")?2:1;
			$data['ngaycap'] = $this->date->formatViewDate($data['ngaycap']);
			if($data['biennhanid']=="")
			{
				if($data['khachhangid'] == "")
				{
					$this->load->model('core/user');
					$user['fullname'] = $data['tenkhachhang'];
					$user['idnumber'] = $data['socmnd'];
					$user['iddate'] = $data['ngaycap'];
					$user['phone'] = $data['sodienthoai'];
					$user['email'] = $data['email'];
					$user['address'] = $data['diachi'];
					$data['khachhangid'] = $this->model_core_user->insertUser($user);
					
					$this->model_core_user->updateCol($data['khachhangid'],'status','active');
					$this->model_core_user->updateCol($data['khachhangid'],'usertypeid','member');
				}
				$data['biennhanid'] = $this->model_camdo_biennhan->insert($data);
			}
			else
			{
				$this->model_camdo_biennhan->update($data);	
			}
			
			
			$biennhan = $this->model_camdo_biennhan->getItem($data['biennhanid']);
			if((int)$biennhan['phieuchiid']==0)
			{
				//Xuat phieu chi cam do
				$phieu['prefix'] = "PC";
				$phieu['loaithuchi'] = "chi";
				$phieu['taikhoanthuchi'] = "chicamdo";
				$phieu['chungtulienquan'] = $biennhan['sobiennhan'];
				$phieu['makhachhang'] = $biennhan['khachhangid'];
				$phieu['tenkhachhang'] = $biennhan['tenkhachhang'];
				$phieu['diachi'] = $biennhan['diachi'];
				$phieu['email'] = $biennhan['sobiennhan'];
				$phieu['dienthoai'] = $biennhan['sodienthoai'];
				$phieu['email'] = $biennhan['email'];
				$phieu['sotien'] = $this->string->toNumber($biennhan['giatricam']);
				$phieu['donvi'] = 'VND';
				$phieu['quidoi'] = $this->document->toVND($this->string->toNumber($phieu['sotien']),$phieu['donvi']);
				$phieu['lydo'] = "Chi tiền cầm đồ số ".$biennhan['sobiennhan']." mã số".$biennhan['masotaisan']." ".$biennhan['motataisan'];
				$phieu['nguongoc'] = $biennhan['biennhanid'];
				$phieu['maphieu'] = $this->model_ben_thuchi->insert($phieu);	
				
				$this->model_camdo_biennhan->updateCol($biennhan['biennhanid'],"maphieuchi",$phieu['maphieu']);
			}
			
			
			
			
			$this->data['output'] = "true-".$data['biennhanid'];
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		
		if (trim($data['tenkhachhang']) == "") 
		{
      		$this->error['tenkhachhang'] = "Bạn chưa nhập tên khách hàng";
    	}
		
		if (trim($data['sodienthoai']) == "") 
		{
      		$this->error['sodienthoai'] = "Bạn chưa nhập tên khách hàng";
    	}
		
		if (trim($data['email']) != "") 
		{
      		if ($this->validation->_checkEmail($data['email']) == false )
				$this->error["email"] = "Email không đúng định dạng";
    	}
		
		if (trim($data['giatricam']) == 0) 
		{
      		$this->error['giatricam'] = "Bạn chưa nhập giá trị cầm";
    	}
		
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	
	public function donglai()
	{
		$data = $this->request->post;	
		$biennhan = $this->model_camdo_biennhan->getItem($data['biennhanid']);
		$ngaydonglai = $this->date->formatViewDate($data['ngaydonglai']);
		$songaydonglai = $data['songaydonglai'];
		//Xuat phieu thu tien dong lai
		$phieuthu['prefix'] = "PT";
		$phieuthu['loaithuchi'] = "thu";
		$phieuthu['taikhoanthuchi'] = "thutienlai";
		$phieuthu['chungtulienquan'] = $biennhan['sobiennhan'];
		$phieuthu['makhachhang'] = $biennhan['khachhangid'];
		$phieuthu['tenkhachhang'] = $biennhan['tenkhachhang'];
		$phieuthu['diachi'] = $biennhan['diachi'];
		$phieuthu['email'] = $biennhan['sobiennhan'];
		$phieuthu['dienthoai'] = $biennhan['sodienthoai'];
		$phieuthu['email'] = $biennhan['email'];
		$phieuthu['sotien'] = $this->string->toNumber($data['lai']);
		$phieuthu['donvi'] = 'VND';
		$phieuthu['quidoi'] = $this->document->toVND($this->string->toNumber($phieuthu['sotien']),$phieuthu['donvi']);
		$phieuthu['lydo'] = "Thu tiền đóng lãi biên nhận ".$biennhan['sobiennhan'];
		$phieuthu['nguongoc'] = $biennhan['biennhanid'];
		$phieuthu['maphieu'] = $this->model_ben_thuchi->insert($phieuthu);	
		
		//Cap nhat ngay dong lai
		$this->model_camdo_biennhan->updateCol($data['biennhanid'],'ngaydonglai',$ngaydonglai);
		//Cap nhat ngay den han dong lai them 30 ngay
		$this->model_camdo_biennhan->updateCol($data['biennhanid'],'ngaydenlai',$this->date->addday($ngaydonglai,30));
		//Ghi lai lich su dong lai
		$lichsu['biennhanid'] = $data['biennhanid'];
		$lichsu['ngaythu'] = $this->date->getTodayNoTime();
		$lichsu['loaithu'] = 'donglai';
		$lichsu['sotien'] = $phieuthu['sotien'];
		$lichsu['maphieuthu'] = $phieuthu['maphieu'];
		$this->model_camdo_biennhan->saveBienNhanLichSu($lichsu);
		
		$this->data['output'] = "true";
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	public function chuoc()
	{
		$data = $this->request->post;	
		$biennhan = $this->model_camdo_biennhan->getItem($data['biennhanid']);
		$ngaydonglai = $this->date->formatViewDate($data['ngaydonglai']);
		$songaydonglai = $data['songaydonglai'];
		//Xuat phieu thu tien dong lai
		$phieuthu['prefix'] = "PT";
		$phieuthu['loaithuchi'] = "thu";
		$phieuthu['taikhoanthuchi'] = "thuvonlai";
		$phieuthu['chungtulienquan'] = $biennhan['sobiennhan'];
		$phieuthu['makhachhang'] = $biennhan['khachhangid'];
		$phieuthu['tenkhachhang'] = $biennhan['tenkhachhang'];
		$phieuthu['diachi'] = $biennhan['diachi'];
		$phieuthu['email'] = $biennhan['sobiennhan'];
		$phieuthu['dienthoai'] = $biennhan['sodienthoai'];
		$phieuthu['email'] = $biennhan['email'];
		$phieuthu['sotien'] = $this->string->toNumber($data['vonlai']);
		$phieuthu['donvi'] = 'VND';
		$phieuthu['quidoi'] = $this->document->toVND($this->string->toNumber($phieuthu['sotien']),$phieuthu['donvi']);
		$phieuthu['lydo'] = "Thu tiền đóng lãi biên nhận ".$biennhan['sobiennhan'];
		$phieuthu['nguongoc'] = $biennhan['biennhanid'];
		$phieuthu['maphieu'] = $this->model_ben_thuchi->insert($phieuthu);	
		
		//Cap nhat ngay dong lai
		$this->model_camdo_biennhan->updateCol($data['biennhanid'],'ngaydonglai',$ngaydonglai);
		$this->model_camdo_biennhan->updateCol($data['biennhanid'],'tinhtrang','completed');
		
		//Ghi lai lich su dong lai
		$lichsu['biennhanid'] = $data['biennhanid'];
		$lichsu['ngaythu'] = $this->date->getTodayNoTime();
		$lichsu['loaithu'] = 'chuoc';
		$lichsu['sotien'] = $phieuthu['sotien'];
		$lichsu['maphieuthu'] = $phieuthu['maphieu'];
		$this->model_camdo_biennhan->saveBienNhanLichSu($lichsu);
		
		$this->data['output'] = "true";
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	public function thanhly()
	{
		$data = $this->request->post;	
		$biennhan = $this->model_camdo_biennhan->getItem($data['biennhanid']);
		$ngaythanhly = $this->date->formatViewDate($data['ngaythanhly']);
		$sotienthanhly = $data['sotienthanhly'];
		
		//Xuat phieu thu tien thanh ly
		$phieuthu['prefix'] = "PT";
		$phieuthu['loaithuchi'] = "thu";
		$phieuthu['taikhoanthuchi'] = "thanhly";
		$phieuthu['chungtulienquan'] = $biennhan['sobiennhan'];
		$phieuthu['makhachhang'] = $biennhan['khachhangid'];
		$phieuthu['tenkhachhang'] = $biennhan['tenkhachhang'];
		$phieuthu['diachi'] = $biennhan['diachi'];
		$phieuthu['email'] = $biennhan['sobiennhan'];
		$phieuthu['dienthoai'] = $biennhan['sodienthoai'];
		$phieuthu['email'] = $biennhan['email'];
		$phieuthu['sotien'] = $this->string->toNumber($sotienthanhly);
		$phieuthu['donvi'] = 'VND';
		$phieuthu['quidoi'] = $this->document->toVND($this->string->toNumber($phieuthu['sotien']),$phieuthu['donvi']);
		$phieuthu['lydo'] = "Thu tiền thanh lý biên nhận ".$biennhan['sobiennhan'];
		$phieuthu['nguongoc'] = $biennhan['biennhanid'];
		$phieuthu['maphieu'] = $this->model_ben_thuchi->insert($phieuthu);	
		
		//Cap nhat ngay thanh ly
		$this->model_camdo_biennhan->updateCol($data['biennhanid'],'ngaythanhly',$ngaythanhly);
		$this->model_camdo_biennhan->updateCol($data['biennhanid'],'tinhtrang','liquidated');
		
		//Ghi lai lich su dong lai
		$lichsu['biennhanid'] = $data['biennhanid'];
		$lichsu['ngaythu'] = $this->date->getTodayNoTime();
		$lichsu['loaithu'] = 'thanhly';
		$lichsu['sotien'] = $phieuthu['sotien'];
		$lichsu['maphieuthu'] = $phieuthu['maphieu'];
		$this->model_camdo_biennhan->saveBienNhanLichSu($lichsu);
		
		$this->data['output'] = "true";
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
}
?>